package com.ophiux.cs.application.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.GetUserPatientsRequest;
import com.ophiux.cs.application.user.dto.GetUserPatientsResponse;
import com.ophiux.cs.application.user.service.IGetUserPatientsAppService;
import com.ophiux.cs.domain.user.model.aggregate.UserPatientInfo;
import com.ophiux.cs.domain.user.model.valueobj.PatientsValueObj;
import com.ophiux.cs.infrastructure.repositories.user.PatientRepository;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.common.StringUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class GetUserPatientsAppService implements IGetUserPatientsAppService {
	@Autowired
	PatientRepository patientRepository;

	@SuppressWarnings("unchecked")
	@Override
	public Result<ResponsePage<GetUserPatientsResponse>> getUserPatients(GetUserPatientsRequest dto) {
		ResponsePage<GetUserPatientsResponse> respPage = new ResponsePage<GetUserPatientsResponse>();
		
		List<GetUserPatientsResponse> resp = new ArrayList<GetUserPatientsResponse>();
		try {
			/**
			 * 1、验证参数是否允许为空
			 * 2、执行业务 获取返回参数 与 返回签名串
			 */
			if(!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			PatientsValueObj value = new PatientsValueObj();
			value = (PatientsValueObj) ShiftUtils.objToNewObj(dto, PatientsValueObj.class);
			
			UserPatientInfo userPatientInfo = patientRepository.getPatientsInfoList(value);
			if(userPatientInfo==null || StringUtil.isEmpty(userPatientInfo.getUSER_ID())) {
				return Result.fail(ResultEnum.NotFundUser);
			}
			
			resp = ShiftUtils.objToNewObjList(userPatientInfo.getPatients(), GetUserPatientsResponse.class);
			
			respPage.setData(resp);
		} catch (Exception e) {
			log.info("程序异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success(respPage);
	}

}
